package com.haitaoit.chengquan.module.center.activity.xuqiu;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.base.ExitApplication;
import com.haitaoit.chengquan.module.center.model.MyXuqiuBaojiaModel;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.DialogUtil;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.CircleImageView;
import com.haitaoit.chengquan.view.QRatingBar;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyXuqiuBaojiaActivity extends BaseActivity {
    @ViewInject(R.id.center_img)
    CircleImageView center_img;
    @ViewInject(R.id.eva_star)
    QRatingBar eva_star;
    @ViewInject(R.id.company_name)
    TextView company_name;
    @ViewInject(R.id.credit_rating)
    TextView credit_rating;
    @ViewInject(R.id.offer_money)
    TextView offer_money;
    @ViewInject(R.id.contact)
    TextView contact;
    @ViewInject(R.id.contact_position)
    TextView contact_position;
    @ViewInject(R.id.contact_information)
    TextView contact_information;
    @ViewInject(R.id.remarks)
    TextView remarks;


    private String id;
    private MyXuqiuBaojiaModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_my_xuqiu_baojia_detail);
        ExitApplication.getInstance().addActivity(this);
        setTitle_V("报价详情");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);


        id = getIntent().getStringExtra("id");
        httpGetuser_demand_offer_model();
    }

    @OnClick({R.id.contact_information, R.id.choose,})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.contact_information:
                Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + model.getContact_information()));
                startActivity(in);
                break;
            case R.id.choose:
                Dialog dlg = DialogUtil.showInfoDialog2(MyXuqiuBaojiaActivity.this,
                        "选定该服务商则需要支付¥" + model.getOffer_money() + "到他的账号完成服务商选择", "再想想", "去支付", new BackCall() {
                            public void deal(int which, Object... obj) {
                                switch (which) {
                                    case R.id.cancel:// 确定
                                        Intent intent = new Intent(MyXuqiuBaojiaActivity.this, PaymentActivity.class);
                                        intent.putExtra("type", "2");
                                        intent.putExtra("order_no", model.getOrder_no());
                                        intent.putExtra("order_amount", model.getOffer_money());
                                        startActivity(intent);
                                        break;
                                }
                                super.deal(which);
                            }
                        });
                dlg.setCanceledOnTouchOutside(true);
                dlg.show();
                break;
        }
        super.onClick(v);
    }

    // 列表
    private void httpGetuser_demand_offer_model() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("id", id));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("id", id);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getuser_demand_offer_model, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String result = arg0.result.toString();
                try {
                    JSONObject json = new JSONObject(result);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    Gson gson = new Gson();
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            break;
                        case 0:// 成功
                            model = gson.fromJson(Response, MyXuqiuBaojiaModel.class);
                            ImageLoader.getInstance().displayImage(model.getHead_portrait(), center_img);
                            company_name.setText(model.getCompany_name());
                            eva_star.setRating(Float.parseFloat(model.getCredit_rating()));
                            credit_rating.setText(model.getCredit_rating());
                            offer_money.setText("¥ " + model.getOffer_money()+"元");
                            contact.setText(model.getContact());
                            contact_position.setText(model.getContact_position());
                            contact_information.setText(model.getContact_information());
                            remarks.setText(model.getRemarks());
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
